<?php 
// Config language (can change it)
$a_Lang['Submit_Review'] = 'Добави мнение за тази фирма';
$a_Lang['Name'] = 'Име';
$a_Lang['Rate'] = 'Рейтинг';
$a_Lang['Email'] = 'Имейл';
$a_Lang['Content'] = 'Съдържание';
$a_Lang['Submit'] = 'Изпрати мнение';
$a_Lang['Please_enter_your_name'] = 'Моля,добавете име';
$a_Lang['Please_enter_your_email_address'] = 'Моля добавете имейл';
$a_Lang['Enter_a_valid_email_address'] = 'Добавете валиден имейл';
$a_Lang['Please_enter_the_content'] = 'Добавете описание';
$a_Lang['Thank_you_for_your_review'] = 'Благодарим ви за мнението!';

?>
<div id="submit-review">
	<h2 style="font-size: 16px;font-weight: bold"><?php echo $a_Lang['Submit_Review']?></h2>
	<div id="review">
		<?php echo $form->create('Review',array('action'=>'reviewtest','class'=>'','id'=>'form-review'));?>
			<table border="0" cellspacing="5" cellpadding="5" width="480px">
				<tr>
		        	<!-- <td>Rating: </td> -->
                                 <td><?php echo $a_Lang['Rate']?>: </td>
		            <td colspan="2">
		            	<div id="rating">
			            	<img id="1" src="<?php echo SITE_WEBROOT; ?>img/stars_disabled.png"/>
			            	<img id="2" src="<?php echo SITE_WEBROOT; ?>img/stars_disabled.png"/>
			            	<img id="3" src="<?php echo SITE_WEBROOT; ?>img/stars_disabled.png"/>
			            	<img id="4" src="<?php echo SITE_WEBROOT; ?>img/stars_disabled.png"/>
			            	<img id="5" src="<?php echo SITE_WEBROOT; ?>img/stars_disabled.png"/>
		            	</div>
		            	<?php echo $form->hidden('rate',array('value'=>'0')); ?>
		            	<?php echo $form->hidden('listing_id',array('value'=>$listing['Listing']['id'])); ?>
		            	<?php echo $form->hidden('category_id',array('value'=>$listing['Listing']['category_id'])); ?>
		            </td>
		        </tr>
		        <tr>
		        	<td><?php echo $a_Lang['Name']?>: </td>
		            <td><?php echo $form->input('name',array('label'=>false,'div'=>'','value'=>'', 'size' => 30)); ?> <font color="red">*</font></td>
		        </tr>
		        <tr>
		        	<td><?php echo $a_Lang['Email']?>: </td>
		            <td><?php echo $form->input('email',array('label'=>false,'div'=>'','value'=>'', 'size' => 30)); ?> <font color="red">*</font></td>
		        </tr>
		        
		         <tr>
                                <td><?php echo $a_Lang['Content']?>: </td>
		            <td colspan="2"><?php echo $form->textarea('content',array('label'=>false, 'rows'=> 5,'cols' => 40 )); ?> <font color="red">*</font></td>
		        </tr>
		        <tr><td colspan="2" align="center">
		        	<input id="submit" type="button" value="<?php echo $a_Lang['Submit']?>" />
		        </td></tr>
			</table>
		<?php 
		    echo $form->end();
		?> 
	</div>
</div>



<script>
	var root_url = '<?php echo SITE_WEBROOT; ?>';
	var rate = 0; 
	$(document).ready(function(){
		$("#rating img").mouseout(function(){
			//$(this).attr('src', root_url + 'img/stars_disabled.png');
			var max = parseInt($(this).attr('id'));
			for( i = 1; i <= max; i++ )
			{
				$('#' + i).attr('src', root_url + 'img/stars_disabled.png');	
			}
		});
		
		$("#rating img").mouseover(function(){
			var _this = this;
			$("#rating img").bind('mouseout', function (){
				$("#rating img").each(function(){
					$(this).attr('src', root_url + 'img/stars_disabled.png');	
				});
			});
			$("#rating img").each(function(){
				$(this).attr('src', root_url + 'img/stars_disabled.png');	
			});
			var max = parseInt($(_this).attr('id'));
			for( i = 1; i <= max; i++ )
			{
				$('#' + i).attr('src', root_url + 'img/stars_enabled.png');	
			}
		});
		$("#rating img").click(function(){
			$("#rating img").unbind('mouseout');
			var max = parseInt($(this).attr('id'));
			for( i = 1; i <= max; i++ )
			{
				$('#' + i).attr('src', root_url + 'img/stars_enabled.png');	
			}
			//$('#ReviewRate').val( max );
			rate = max;
		});
	});

	var your_name = "<?php echo $a_Lang['Please_enter_your_name']?>";
	var your_email = "<?php echo $a_Lang['Please_enter_your_email_address']?>";
	var invalid_email = "<?php echo $a_Lang['Enter_a_valid_email_address']?>";
	var your_content = "<?php echo $a_Lang['Please_enter_the_content']?>";
	var review_success = "<?php echo $a_Lang['Thank_you_for_your_review']?>";
    function v_fValidate()
    {
        // Validate name
    	var sz_Name = $('#ReviewName').val();
    	if( sz_Name == ""){
        	alert(your_name);
        	$('#ReviewName').focus();
        	return false;
    	}
    	// Validate email
    	var sz_Email = $('#ReviewEmail').val();
    	var sz_EmailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    	if ( sz_Email == "" )
    	{
    		alert(your_email);
    		$('#ReviewEmail').focus();
    		return false;
        }
        if ( !sz_EmailReg.test(sz_Email) )
        {
            alert(invalid_email);
            $('#ReviewEmail').focus();
            return false;
		}
		// Validate content
		var sz_Content = $('#ReviewContent').val();
    	if( sz_Content == "" ){
        	alert(your_content);
        	$('#ReviewContent').focus();
        	return false;
    	}
    	// Validate rating
    	if ( parseInt(rate) == 0 )
    	{
        	alert('Please select rating');
        	return false;
        }
    	return true;
	}
	$(document).ready(function (){
		$("#submit").click(function(){
			$('input#submit').attr('disabled',true);
			if ( !v_fValidate()) {
				$('input#submit').attr('disabled', false);
				return false;
			} else {
			// call submit review

				$.ajax({
					type: 'POST',
					url: '<?php echo SITE_WEBROOT?>' + 'listings/review?rate=' + rate,
					data:$('#form-review').serializeArray(),
					dataType: 'json',
					success: function(data){
						if( data.i_Code == 1 )
						{
							$("#review").html('<p style="color: red; font-size:16;margin-top:20px">' + review_success + '</p>');
						}
					}
				}).fail(function(message){
				$('input#submit').attr('disabled', false);
					alert('Please login to post review');
					});
			}
		});
	});
    
</script>
<style>
	#submit-review {
		margin-top: 10px;
	}
	#submit-review table {
		border-collapse: separate;
		border-spacing: 5px !important;
		padding-left: 0px !important;
		padding-top: 15px !important;
	}
	#submit-review table td {
		vertical-align: middle;
	}
	#submit-review input, #submit-review textarea {
		
		padding: 2% !important;
		font-size: 100% !important;
	}
	#submit-review input {
		width: 250px !important;
	}
	#submit-review textarea {
		width: 370px !important;
		height: 50px !important;
	}
	#submit-review #submit {
		width: auto !important;
		padding: 1% !important;
	}
</style>